<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备编号" prop="doorCode">
      <el-input v-model="dataForm.doorCode" placeholder="设备编号"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="doorName">
      <el-input v-model="dataForm.doorName" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="门禁厂家" prop="doorFactory">
      <el-input v-model="dataForm.doorFactory" placeholder="门禁厂家"></el-input>
    </el-form-item>
    <el-form-item label="所属区域" prop="doorArea">
      <el-input v-model="dataForm.doorArea" placeholder="所属区域"></el-input>
    </el-form-item>
    <el-form-item label="设备状态" prop="doorStatus">
      <el-select v-model="dataForm.doorStatus" placeholder="请选择">
        <el-option
          v-for="item in doorStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="ip地址" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="ip地址"></el-input>
    </el-form-item>
    <el-form-item label="端口号" prop="port">
      <el-input v-model="dataForm.port" placeholder="端口号"></el-input>
    </el-form-item>
    <el-form-item label="账号" prop="username">
      <el-input v-model="dataForm.username" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/doordevice'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        doorStatus: [
          {
          value: '1',
          label: '返修'
        }, {
          value: '2',
          label: '使用中'
        }],
        dataForm: {
          id: '',
          doorName:'',
          doorCode: '',

          doorFactory: '',

          doorArea: '',

          doorStatus: '',

          ip: '',

          port: '',

          username: '',

          password: '',

          createTime: '',

          updateTime: '',

          delFlag: '',

        },
        dataRule: {
          doorCode: [
            { required: true, message: '设备编号不能为空', trigger: 'blur' }
          ],

          doorName: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],

          doorFactory: [
            { required: true, message: '门禁厂家不能为空', trigger: 'blur' }
          ],

          doorArea: [
            { required: true, message: '设备状态不能为空', trigger: 'blur' }
          ],

          doorStatus: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],

          ip: [
            { required: true, message: 'ip地址不能为空', trigger: 'blur' }
          ],

          port: [
            { required: true, message: '端口号不能为空', trigger: 'blur' }
          ],

          username: [
            { required: true, message: '账号不能为空', trigger: 'blur' }
          ],

          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
